﻿
@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 35% !important;
    }
</style>
<div ng-controller="UserGroupController">
    <h3>权限管理</h3>

    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading}">
        <div class="panel-heading">
            <div class="form-inline ng-pristine ng-valid">
                <div class="form-group">
                    <input id="input-email" placeholder="分组名称" class="form-control" type="text" ng-model="Keywords">
                    <button type="submit" class="btn btn-labeled btn-primary" ng-click="tableParams.$params.page = 1;tableParams.reload()">
                        <span class="btn-label">
                            <em class="fa fa-search"></em>
                        </span>搜索
                    </button>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-labeled btn-success" ng-click="positionManagementUpdataView(0)">
                        <span class="btn-label">
                            <em class="fa fa-plus"></em>
                        </span>新增
                    </button>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                    <tbody>
                        <tr ng-repeat="employeerole in $data">
                            <td data-title="'编号'" class="text-center">{{employeerole.Id}}</td>
                            <td data-title="'分组名称'" class="text-center">{{employeerole.GroupName}}</td>
                            <td data-title="'分组描述'" class="text-center">{{employeerole.Describe}}</td>
                            <td data-title="'对应帐号'" class="text-center"><span class="mr-sm label label-primary ng-binding" ng-repeat="username in employeerole.UserNameList" style="word-wrap:break-word; display:inline-block;">{{username}}</span></td>
                            <td data-title="'添加时间'" class="text-center">{{employeerole.AddTime}}</td>
                            <td data-title="'操作'" nowrap="nowrap" class="text-center">
                                <button type="button" ng-click="positionManagementUpdataView(employeerole)" class="btn btn-sm btn-edit" title="编辑">
                                    <em class="fa fa-edit"></em>
                                </button>
                                <button ng-show="{{!employeerole.IsAdmin}}" type="button" ng-click="positionManagementView(employeerole.Id)" class="btn btn-sm btn-primary" title="修改权限">
                                    <em class="icon-key"></em>
                                </button>
                                <button ng-show="{{!employeerole.IsAdmin}}" type="button" ng-click="removePositionCenter(employeerole)" class="btn btn-sm btn-danger" title="删除">
                                    <em class="fa fa-trash-o"></em>
                                </button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script type="text/ng-template" id="getJurisdictionDialogId">
    <div class="ngdialog-message">
        <div class="panel panel-default">
            <div class="panel-body">
                <fieldset class="b0">
                    <legend>权限管理</legend>
                </fieldset>
                <fieldset>
                    <div id="authTree" js-tree="vm.treeConfig" should-apply="vm.applyModelChanges()" ng-model="vm.treeData" tree="vm.treeInstance" tree-events="ready:vm.readyCB;create_node:vm.createCB"></div>
                </fieldset>
                <div class="panel-footer text-right">
                    <button type="button" class="btn btn-default" ng-click="closeThisDialog()">
                        关闭
                    </button>
                    <button type="button" class="btn btn-default" ng-click="serveRootMessage()">
                        确定
                    </button>
                    <button type="button" style="display:none" class="btn btn-default" ng-click="getChecked()">
                        获取
                    </button>
                </div>
            </div>
        </div>
    </div>
</script>

<script type="text/ng-template" id="setUpTempDialogId">
    <div class="ngdialog-message">
        <div class="panel panel-default">
            <div class="panel-body">
                <fieldset class="b0">
                    <legend>{{Position.Id == 0 || Position.Id == null ? '添加' : '修改'}}</legend>
                </fieldset>
                <fieldset>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">分组名称</label>
                        <div class="col-sm-6">
                            <input type="text" name="Name" required="" maxlength="20" ng-model="Position.GroupName" class="form-control" />
                        </div>
                        <div class="col-sm-1">
                            <h4 class="text-danger">*</h4>
                        </div>
                    </div>
                </fieldset>
                <fieldset>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">描述</label>
                        <div class="col-sm-8">
                            <textarea ng-model="Position.Describe" class="form-control" rows="6"></textarea>
                        </div>
                    </div>
                </fieldset>
                <fieldset>
                    <div class="form-group" ng-show="{{!Position.IsAdmin}}">
                        <label class="col-sm-4 control-label">权限修改</label>
                        <div class="col-sm-8">
                            <input type="checkbox" ng-checked="{{Position.IsAdd}}" ng-click="changeAuthority(Position,'IsAdd')"/>新增
                            <input type="checkbox" ng-checked="{{Position.IsEdit}}" ng-click="changeAuthority(Position,'IsEdit')"/>修改
                            <input type="checkbox" ng-checked="{{Position.IsQuery}}" ng-click="changeAuthority(Position,'IsQuery')"/>查询
                            <input type="checkbox" ng-checked="{{Position.IsDel}}" ng-click="changeAuthority(Position,'IsDel')"/>删除
                        </div>
                    </div>
                </fieldset>
                <div class="panel-footer text-right">
                    <button type="button" class="btn btn-default" ng-click="closeThisDialog()">
                        关闭
                    </button>
                    <button type="button" class="btn btn-default" ng-click="serveThisMessage()">
                        确定
                    </button>
                </div>
            </div>
        </div>
    </div>
</script>
<script type="text/ng-template" id="deleteConfirmDialog">
    <div class="ngdialog-message  text-center">
        <h3>确定要删除吗?</h3>
        <p>删除后该分组下所有员工权限将不可用且不可恢复!</p>
        <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
        <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
    </div>
</script>
<script>
    App.controller('UserGroupController', [
    '$scope', '$resource', '$state', '$route', '$http', 'ngTableParams', 'ngTableDataService', 'Notify', '$filter', 'ngDialog', "$stateParams", "$timeout", "toaster",
        function ($scope, $resource, $state, $route, $http, ngTableParams, ngTableDataService, Notify, $filter, ngDialog, $stateParams, $timeout, toaster) {
            'use strict';
            $scope.isLoading = true;
            //数据查询
            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 10,
                sorting: {
                    Id: 'desc'     // 初始化排序
                }
            }, {
                getData: function ($defer, params) {
                    $scope.isLoading = true;
                    var requestParams = {
                        Skip: (params.page() - 1) * params.count(),
                        Take: params.count(),
                        Keywords: $scope.Keywords
                    };
                    $http.post('/User/UserGroupManage', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                        .success(function (data, status) {
                            params.total(data.Total);
                            console.log(JSON.stringify(data))
                            $defer.resolve(data.Results);
                            $scope.isLoading = false;
                        });
                }
            });

            //跳转更新新增页面
            $scope.positionManagementUpdataView = function (positionCenter) {
                if (positionCenter == 0) {
                    $scope.Position = {};
                } else {
                    $scope.Position = angular.copy(positionCenter);
                }
                ngDialog.openConfirm({
                    template: 'setUpTempDialogId',
                    className: 'ngdialog-theme-plain',
                    scope: $scope
                }).then(function (value) {
                }, function (reason) {
                }
                );
            }
            //确认点击(添加职称)
            $scope.serveThisMessage = function () {
                if ($scope.Position.GroupName == '' || $scope.Position.GroupName == null) {
                    Notify.alert("请填写名称", { status: 'warning' });
                    return false;
                }
                $http.post("/User/UserGroupEdit", { userGroup: $scope.Position }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success' });
                        ngDialog.closeAll();
                        $scope.tableParams.$params.page = 1;
                        $scope.tableParams.reload();
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }
                });
            }

            $scope.getChecked = function () {
                alert($("#authTree").jstree().get_checked());
            }

            //点击修改权限
            $scope.positionManagementView = function (groupid) {
                $scope.PositionId = groupid;
                $http.post("/User/GetMenuList", { groupId: groupid }).then(function (returnData) {
                    returnData.data.forEach(function (e) {
                        e.id = e.Id.toString();
                    });
                    $scope.vm.treeData = returnData.data;
                    ngDialog.openConfirm({
                        template: 'getJurisdictionDialogId',
                        className: 'ngdialog-theme-plain',
                        scope: $scope
                    }).then(function (value) {
                    }, function (reason) {
                    });
                });
            }

            //确认点击(权限管理)
            $scope.serveRootMessage = function () {
                $http.post("/User/UpdateGroupRole", { AuthList: $("#authTree").jstree().get_checked(), GroupId: $scope.PositionId }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success' });
                        ngDialog.closeAll();
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }
                });
            }
            //删除职位
            $scope.removePositionCenter = function (positionCenter) {
                ngDialog.openConfirm({
                    template: 'deleteConfirmDialog',
                    className: 'ngdialog-theme-default',
                    scope: $scope
                }).then(function (value) {
                    $scope.isLoading = true;
                    $http.post("/User/DelGroupRole", { Id: positionCenter.Id }).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            Notify.alert('操作成功!', { status: 'success' });
                            $scope.tableParams.$params.page = 1;
                            $scope.tableParams.reload();
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning' });
                        }
                        $scope.isLoading = false;
                    });
                }, function (reason) {
                }
                );
            }

            //权限树初始化
            $scope.vm = {
                treeData: [],
                treeConfig: {
                    core: {
                        multiple: true,
                        animation: true,
                        error: function (error) {
                            $log.error('treeCtrl: error from js tree - ' + angular.toJson(error));
                        },
                        check_callback: true,
                        worker: true
                    },
                    types: {
                        default: {
                            icon: 'fa fa-book'
                        },
                        star: {
                            icon: 'glyphicon glyphicon-star'
                        },
                        cloud: {
                            icon: 'glyphicon glyphicon-cloud'
                        }
                    },
                    version: 1,
                    plugins: ['types', 'checkbox']
                },
                applyModelChanges: function () {
                    return !$scope.vm.ignoreChanges;
                },
                setNodeType: function () {
                    var item = _.findWhere(this.treeData, { id: this.selectedNode });
                    item.type = this.newType;
                    toaster.pop('success', 'Node Type Changed', 'Changed the type of node ' + this.selectedNode);
                },
                readyCB: function () {
                    $timeout(function () {
                        $scope.vm.ignoreChanges = false;
                        toaster.pop('success', 'JS Tree Ready', 'Js Tree issued the ready event');
                    });
                },
                createCB: function (e, item) {
                    $timeout(function () { toaster.pop('success', 'Node Added', 'Added new node with the text ' + item.node.text) });
                }
            };

            $scope.changeAuthority = function (Position, authority) {
                if (authority == 'IsAdd') {
                    if (Position.IsAdd == true) {
                        $scope.Position.IsAdd = false;
                    }
                    else {
                        $scope.Position.IsAdd = true;
                    }
                    
                }else if (authority == 'IsQuery') {
                    if (Position.IsQuery == true) {
                        $scope.Position.IsQuery = false;
                    }
                    else {
                        $scope.Position.IsQuery = true;
                    }
                }else if (authority == 'IsEdit') {
                    
                    if (Position.IsEdit == true) {
                        $scope.Position.IsEdit = false;
                    }
                    else {
                        $scope.Position.IsEdit = true;
                    }
                }else if (authority == 'IsDel') {
                    if (Position.IsDel == true) {
                        $scope.Position.IsDel = false;
                    }
                    else {
                        $scope.Position.IsDel = true;
                    }
                }
            };
        }
    ]);
</script>
